#include<stdio.h>
#include "../include/vfs.h"
#include<stdlib.h>
#include<string.h>
#include "../include/mount_vfs.h"



int mount_vfs(char vfs_name[])
{

	FILE *fp,*test;
	int i=0,choice,j;

	List *l;
	FD *fd,*temp;
	Block *b;
	
	/*if(strcmp(vfs_name,filesystem_name)!=0)
		return 2;
	*/
	
	fp = fopen(vfs_name,"r+b");

	if(fp==NULL)
	{	
		return 1;
	}	

	header=malloc(sizeof(Header));

	fread(header,sizeof(Header),1,fp);


	if(header==NULL)
		return 3;

	for(i=0;i<header->used_FD;i++)
	{
		if(i==0)
		{
		fd=(FD*)malloc(sizeof(FD));
		strcpy(fd->filename,header->fd_list[i].filename);
		strcpy(fd->path,header->fd_list[i].path);
		fd->file_type = header->fd_list[i].file_type;
		fd->file_size=header->fd_list[i].file_size;
		fd->block_no=header->fd_list[i].block_no;


			roots.naryroot=add_root(roots.naryroot,fd);
			roots.bstRoot = insert(roots.bstRoot,fd);
			hash_table();
		}

		else{



		fd=(FD*)malloc(sizeof(FD));
		strcpy(fd->filename,header->fd_list[i].filename);
		strcpy(fd->path,header->fd_list[i].path);
		fd->file_type = header->fd_list[i].file_type;
		fd->file_size=header->fd_list[i].file_size;
		fd->block_no=header->fd_list[i].block_no;

			
				roots.naryroot=add_node(roots.naryroot,fd,0);
				roots.bstRoot = insert(roots.bstRoot,fd);
			
				if(fd->file_type>0)
					hashtable_insert(fd);
		}
		
		
	}

	strcpy(filesystem_name,vfs_name);
	
	//display(roots.naryroot);

//****************************************************************************************************************************************
	
	//printf("\n MOUNTED SUCCESSFULLY %s",vfs_name);
	fclose(fp);

	


	return 0;

}
